<?php

namespace App\Http\Controllers\Api;

use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;

class IncomeAnalysisController extends AppBaseController
{

    //
    public function __invoke(Request $request)
    {
        // 参数验证
        $this->validate($request, [
            'park_code'   => 'required|array',
            'park_code.*' => 'required|numeric',
            'start_date'  => 'required|date',
            'end_date'    => 'required|date|after_or_equal:start_date'
        ]);
        $park_codes = $request->park_code;
        $date_struct = $this->convertDateTime($request->start_date, $request->end_date); // 查询时间转换
        $model = new \App\Models\DateCollection;

        // 收入合计
        $data['income'] = $model->collectIncome($park_codes, $date_struct);
        // 支付类型
        $data['pay_type'] = $model->collectPayType($park_codes, $date_struct);
        // 车辆类型
        $data['car_type'] = $model->collectCarType($park_codes, $date_struct);

        return $this->sendResponse($data, '查询成功！');
    }

    /**
     * 查询时间范围转换(结构体)
     *
     * @param [type] $start_date
     * @param [type] $end_date
     * @return void
     */
    private function convertDateTime($start_date, $end_date)
    {
        $stime = strtotime($start_date);
        $etime = strtotime($end_date);

//        if(date('Y-m-d 00:00:00', $stime) == date('Y-m-d 00:00:00', $etime)){    // 粒度：天
//            $etime += 3600 * 24;
//            $left  = '[';
//            $right = ')';
//        }elseif(date('Y-m-d H:00:00', $stime) == date('Y-m-d H:00:00', $etime)){  // 粒度：小时
//            $etime += 3600;
//            $left  = '[';
//            $right = ')';
//        }else{  // 粒度：未知
//            $left  = '[';
//            $right = ']';
//        }
        $left  = '[';
        $right = ']';
        $res =  [
            $left,
            date('Y-m-d H:i:s', $stime),
            date('Y-m-d H:i:s', $etime),
            $right
        ];
        return $res;
    }
}
